package dymnd.image;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.Serializable;
public class Layer extends BufferedImage implements Serializable{
	static final long serialVersionUID = 1;
	private String name = "New Layer";
	private boolean modified = false;
	private long id = 0x0;
	public boolean visible = true;
	public int index = 0;
	
	//private int index = 0;
	
	
	//LinkedList<Packet> packets = new LinkedList<Packet>();
	
	public final long getId()	{
		return id;
	}
	
	private String owner = null;
	
	
	public Layer(int width, int height, String owner)	{
		super(width, height, BufferedImage.TYPE_INT_ARGB);
		this.owner = owner;
		this.id = LayerCounter.getInstance().getNextID();
		System.out.println("Id: " +this.id);
		clear();
	}
	
	/*private Layer(int width, int height)	{
		super(width, height, BufferedImage.TYPE_INT_ARGB);
		clear();
	}*/
	
	public boolean isPublic()	{
		return owner == null;
	}
	
	public String getOwner()	{
		return owner;
	}
	
	
	public void clear()	{
		Graphics2D g = createGraphics();
		g.setComposite(AlphaComposite.Src);
		g.setColor(new Color(0, 0, 0, 0));
		g.fillRect(0, 0, getHeight(), getWidth());
		g.dispose();
	}
	
	
	/*public LinkedList<Packet> getPackets()	{
		return packets;
	}
	*/
	/*public void addPacket(Packet p)	{
		packets.add(p);
	}*/
	
	public String getName() {
		if(name == null)
			return "unnamed";
		return name;
	}
	
	public Layer setIndex(int i)	{
		this.index = i;
		return this;
	}

	public Layer setName(String name) {
		this.name = name;
		return this;
	}

	public boolean isModified() {
		return modified;
	}

	public void setModified(boolean modified) {
		this.modified = modified;
	}

	/*public int getIndex() {
		return index;
	}*/

	/*public void setIndex(int index) {
		this.index = index;
	}*/

	public Layer clone()	{
		Layer l = new Layer(this.getWidth(), this.getHeight(), this.owner);
		l.index = this.index;
		l.modified = this.modified;
		l.name = this.name;
		l.owner = this.owner;
		l.visible = this.visible;
		l.setData(this.getData());
		l.id = this.id;
		return l;
	}
	
	public String toString()	{
		return name;
	}
	
	
	
	
}

class LayerCounter {
	private long idCount = 0;
	private LayerCounter()	{
		
	}
	
	public long getNextID()	{
		return idCount++;
	}
	private static class Holder	{
		private static LayerCounter INSTANCE = new LayerCounter();
	}
	
	public static LayerCounter getInstance()	{
		return Holder.INSTANCE;
	}
	
}



